package net.techfuser.bigscreen;

import com.alibaba.fastjson.JSONObject;
import net.techfuser.bigscreen.entity.Summary;
import org.apache.flink.api.common.functions.AggregateFunction;

/**
 * @Description:
 * @Author 左龙龙
 * @Date 21-4-6
 * @Version 1.0
 **/ // *
//         * @param <IN> The type of the values that are aggregated (input values)
//        * @param <ACC> The type of the accumulator (intermediate aggregate state).
//        * @param <OUT> The type of the aggregated result
//        */
//@PublicEvolving
//public interface AggregateFunction<IN, ACC, OUT> extends Function, Serializable {
class OrderAggregate implements AggregateFunction<JSONObject, Summary, Summary> {

    @Override
    public Summary createAccumulator() {
        return new Summary();
    }

    @Override
    public Summary add(JSONObject value, Summary accumulator) {
        accumulator.total_sale_value += value.getDoubleValue("total_sale_value");
        accumulator.count++;

        return accumulator;
    }

    @Override
    public Summary getResult(Summary accumulator) {
        return accumulator;
    }

    @Override
    public Summary merge(Summary a, Summary b) {
        a.count += b.count;
        a.total_sale_value += b.total_sale_value;
        return a;
    }
}
